package org.jeecg.modules.power.records.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.app.v1.device.entity.ApiDeviceOfflineEntity;
import org.jeecg.modules.app.v1.home.entity.*;
import org.jeecg.modules.power.records.entity.YlDeviceStatusRecords;
import org.jeecg.modules.power.records.mapper.YlDeviceStatusRecordsMapper;
import org.jeecg.modules.power.records.service.IYlDeviceStatusRecordsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 设备空仓、满仓、离线时长统计
 * @Author: jeecg-boot
 * @Date:   2024-07-31
 * @Version: V1.0
 */
@Service
public class YlDeviceStatusRecordsServiceImpl extends ServiceImpl<YlDeviceStatusRecordsMapper, YlDeviceStatusRecords> implements IYlDeviceStatusRecordsService {

    /**
     * app首页设备信息统计
     * @param categoryId
     * @param flag
     * @param groupId
     * @return
     */
    @Override
    public ApiHomeDeviceEntity countDeviceForHome(String categoryId, String flag, String groupId) {
        return baseMapper.countDeviceForHome(categoryId, flag, groupId);
    }

    @Override
    public ApiEmptyDeviceEntity countEmptyDevice( DeviceParam param) {
        return baseMapper.countEmptyDevice(param);
    }

    /**
     * 设备空仓，满仓、离线页面数据
     * @param page
     * @param param
     * @return
     */
    @Override
    public List<ApiEmptyDeviceEntityExtends> shopDevice(Page<ApiEmptyDeviceEntityExtends> page, DeviceParam param) {
        String type = param.getType()+param.getOrderBy();
        param.setType(type);
        IPage<ApiEmptyDeviceEntityExtends> iPage = baseMapper.shopDevice(page,param);
        List<ApiEmptyDeviceEntityExtends> list = iPage.getRecords();
        list.forEach(e->{
            DeviceParam params = new DeviceParam();
            BeanUtils.copyProperties(params,param);
            params.setShopId(e.getShopId());
            List<Device> deviceList = shopEmptyDevice(params);
            e.setDeviceList(deviceList);
        });
        return list;
    }

    @Override
    public List<Device> shopEmptyDevice(DeviceParam param) {
        return baseMapper.shopEmptyDevice(param);
    }

    @Override
    public ApiHomeDeviceEntity countShopDevice(String shopId) {
        return baseMapper.countShopDevice(shopId);
    }

    @Override
    public IPage<ApiDeviceOfflineEntity> offlineRecords(Page<ApiDeviceOfflineEntity> page, String deviceId) {
        return baseMapper.offlineRecords(page, deviceId);
    }

}
